<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--头部-->
      <div v-if="true" style="padding-bottom: 10px;">
        <!-- 搜索 -->
        <el-select
          class="main-select-tree filter-item"
          ref="selectTree"
          v-model="optionId"
          placeholder="设备位置"
          style="width: 320px;">
          <el-option v-for="item in formatData(factoryData)"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value" style="display: none;" />

          <el-tree class="main-select-el-tree" ref="selecteltree"
                   :data="factoryData"
                   node-key="id"
                   highlight-current
                   :props="defaultProps"
                   @node-click="handleNodeClick"
                   :expand-on-click-node="expandOnClickNode"
                   default-expand-all />

        </el-select>
        <el-input v-model="query.deviceCode" clearable placeholder="设备编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-input v-model="query.deviceName" clearable placeholder="设备名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select
          v-model="query.deviceStatus"
          clearable
          filterable
          placeholder="请选择运行状态"
          style="width: 185px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.device_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"/>
        </el-select>

        <el-select
          v-model="query.isOnline"
          clearable
          filterable
          placeholder="请选择在线状态"
          style="width: 185px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.is_online"
            :key="item.id"
            :label="item.label"
            :value="item.value"/>
        </el-select>

        <rrOperation :crud="crud" > </rrOperation>
      </div>

     <!--<crudOperation :permission="permission" />-->

      <!--无数据-->
      <el-empty v-show="!crud.data || crud.data.length == 0" description="暂无数据"></el-empty>
      <!--渲染列表-->
      <el-row :gutter="20" v-show="crud.data && crud.data.length > 0" v-loading="crud.loading" >
        <el-col :span="6" v-for="(item,index) in crud.data" :key="item.id">
          <div class="grid-content bg-purple">
            <el-card class="box-card">
              <div class="top">
                <img :src="item.picture ? baseApi + '/' +item.picture :  defaultUrl " class="img">
                <div class="content">
                  <div class="titleName">{{item.deviceName}}</div>
                  <div class="subTitle">设备编码: {{item.deviceCode}}</div>
                  <div class="descWrapper">
                    <div class="descTitle">在线状态: </div>
                    <div class="descContent">
                      <el-tag :type="getIsOnlineColor(item)">{{ dict.label.is_online[item.isOnline] }}</el-tag>
                    </div>
                  </div>
                  <div class="descWrapper">
                    <div class="descTitle">运行状态: </div>
                    <div class="descContent">
                      <el-tag :type="getStatusColor(item)">
                        {{ dict.label.device_status[item.deviceStatus] }}
                      </el-tag>
                    </div>
                  </div>
                </div>
              </div>
              <div class="bottom">
                <router-link :to="'/device/monitor/detail/'+item.deviceCode" >
                  <el-button class="button" size="mini">
                    查看
                  </el-button>
                </router-link>
                <div class="time">报警代码: {{item.alarmCode}}</div>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
      <!--分页-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDeviceMain from '@/api/device/deviceMain'
import crudFactoryModel from "@/api/factory/factoryModel";
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import device from '@/assets/images/device1.png'



  const defaultForm = {
    id: null,
    deviceCode: null,
    deviceName: null,
    deviceType: null,
    deviceModel: null,
    lineCode: null,
    sectionCode: null,
    stationCode: null,
    receiveOrder: null,
    monitor: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  export default {
  name: 'DeviceMonitor',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_online', 'device_status'],
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  cruds() {
    const crud = CRUD({
      title: '设备监控',
      url: 'api/deviceMain/monitor',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudDeviceMain }});
    crud.page.size = 40;
    return crud;
  },
  data() {
    return {
      expandOnClickNode: true,
      options:[],
      optionId: '',
      factoryData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      transferData : [],
      transferValue: [],
      monitorDialog: false,
      submitLoading: false,
      defaultUrl: device,
      permission: {
        add: ['admin', 'deviceMonitor:add'],
        edit: ['admin', 'deviceMonitor:edit'],
        del: ['admin', 'deviceMonitor:del']
      },
      rules: {
        deviceCode: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        equipmentname: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        linecode: [
          { required: true, message: '产线编码不能为空', trigger: 'blur' }
        ],
        sectioncode: [
          { required: true, message: '工段编码不能为空', trigger: 'blur' }
        ],
        stationcode: [
          { required: true, message: '工位代码不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态代码不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'deviceCode', display_name: '设备编码' },
        { key: 'equipmentname', display_name: '设备名称' },
        { key: 'linecode', display_name: '产线编码' },
        { key: 'sectioncode', display_name: '工段编码' },
        { key: 'stationcode', display_name: '工位代码' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.monitor = '1';
      return true
    },
    //重置之后
    [CRUD.HOOK.afterReset](crud) {
      this.optionId = '';
      return true
    },
    //初始化设备树
    initDeviceTree(){
      crudFactoryModel.getTreeNotDevice().then(res =>{
        this.factoryData = res;
      })
    },
    // 四级菜单
    formatData(data){
      let options = [];
      //递归解析
      this.pushOptions(options,data,'');
      this.options = options;
      return options;
    },
    //递归解析
    pushOptions(options,data,label){
      data.forEach((item,key) => {
        let name = label;
        if(name == ''){
          name = item.label;
        } else {
          name = name + "/" + item.label;
        }
        options.push({label:name,value:item.id});
        if(item.children){
          this.pushOptions(options,item.children,name);
        }
      });
    },
    handleNodeClick(node){
      this.optionId = node.id;
      this.options.forEach(item =>{
        if(item.value == node.id){
          this.query.place = item.label;
        }
      })
      this.$refs.selectTree.blur();
      this.crud.toQuery();
    },
    filterMethod(query, item) {
      return item.label.indexOf(query) > -1;
    },
    //关闭
    dialogClose(){
      this.monitorDialog = false;
    },
    //提交
    submitMonitor(){
      this.submitLoading = true;
      crudDeviceMain.updateMonitor(this.transferValue).then(res =>{
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success'
        });
        this.monitorDialog = false;
        this.submitLoading = false;
        this.crud.toQuery();
      })
    },
    getIsOnlineColor(item){
      if(item.isOnline == '0' || item.isOnline == 'FALSE'){
        return 'info';
      }else{
        return '';
      }
    },
    getStatusColor(item){
      if(item.deviceStatus == '1' ){
        return 'success';
      }else if(item.deviceStatus == '2'){
        return 'primary';
      }else if(item.deviceStatus == '3'){
        return 'warning';
      }else if(item.deviceStatus == '4'){
        return 'danger';
      }else if(item.deviceStatus == '5'){
        return 'warning';
      }else if(item.deviceStatus == '6'){
        return 'info';
      }
    }
  },
  mounted() {
    this.initDeviceTree();
  }
}
</script>

<style scoped>
  .header-bottom {
    padding-bottom: 10px;
  }
  .el-card {
    margin-bottom:15px;
  }
  .top {
    width: 100%;
    margin-bottom: 0px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    justify-content: flex-start;
    align-items: flex-start;
    font-family: PingFangSC-Regular;
  }
  .img {
    width: 108px;
    height: 100px;
    margin-right: 15px;
  }
  .content {
    overflow: hidden;
    flex: 1 1;
  }
  .titleName {
    font-size: 14px;
    color: #333;
    line-height: 22px;
    font-weight: 700;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .subTitle {
    font-size: 12px;
    color: #555;
    line-height: 20px;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .descWrapper {
    font-size: 12px;
    color: #555;
    line-height: 20px;
    margin-bottom: 8px;
    display: flex;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .descContent {
    /*white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;*/
    padding-left: 10px;
  }
  .bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    justify-content: flex-start;
  }
  .bottom .time {
    font-size: 13px;
    color: #999;
  }
  .bottom .button {
    float: right;
    margin-right: 20px;
  }

  .edit_dev >>> .el-transfer-panel {
    width:280px;
  }

</style>
